package com.wzx.data.controller;

import com.wzx.basics.baseVo.PageVo;
import com.wzx.basics.baseVo.Result;
import com.wzx.basics.utils.PageUtil;
import com.wzx.basics.utils.ResultUtil;
import com.wzx.data.utils.NullUtils;
import com.wzx.data.entity.Demo;
import com.wzx.data.service.IDemoService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 测试 前端控制器
 * @author wzx
 * @since 2023/03/20
 */
@Slf4j
@RestController
@Api(tags = "测试管理接口")
@RequestMapping("/wzx/demo")
@Transactional
public class DemoController {

    @Autowired
    private IDemoService iDemoService;





    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
    @ApiOperation(value = "查询测试")
    public Result<IPage<Demo>> getByPage(@ModelAttribute Demo demo ,@ModelAttribute PageVo page){
        QueryWrapper<Demo> qw = new QueryWrapper<>();
        if(!NullUtils.isNull(demo.getCreateBy())) {
        }
        IPage<Demo> data = iDemoService.page(PageUtil.initMpPage(page),qw);
        return new ResultUtil<IPage<Demo>>().setData(data);
    }

    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "增改测试")
    public Result<Demo> saveOrUpdate(Demo demo){
        if(iDemoService.saveOrUpdate(demo)){
            return new ResultUtil<Demo>().setData(demo);
        }
        return ResultUtil.error();
    }

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ApiOperation(value = "新增测试")
    public Result<Demo> insert(Demo demo){
        iDemoService.saveOrUpdate(demo);
        return new ResultUtil<Demo>().setData(demo);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation(value = "编辑测试")
    public Result<Demo> update(Demo demo){
        iDemoService.saveOrUpdate(demo);
        return new ResultUtil<Demo>().setData(demo);
    }

    @RequestMapping(value = "/delByIds", method = RequestMethod.POST)
    @ApiOperation(value = "删除测试")
    public Result<Object> delByIds(@RequestParam String[] ids){
        for(String id : ids){
            iDemoService.removeById(id);
        }
        return ResultUtil.success();
    }
}
